<?php

/**
 * @author jzh
 * @copyright 2008
 * @var $im image
 * 		$maxwidth 	max x
 * 		$maxheight 	max y   
 */
function CreateResizedImage($im, $maxwidth, $maxheight)
{
    $width = imagesx($im);
    $height = imagesy($im);
    if (($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)) {
        if ($maxwidth && $width > $maxwidth) {
            $widthratio = $maxwidth / $width;
            $RESIZEWIDTH = true;
        }
        if ($maxheight && $height > $maxheight) {
            $heightratio = $maxheight / $height;
            $RESIZEHEIGHT = true;
        }
        if ($RESIZEWIDTH && $RESIZEHEIGHT) {
            if ($widthratio < $heightratio) {
                $ratio = $widthratio;
            } else {
                $ratio = $heightratio;
            }
        } elseif ($RESIZEWIDTH) {
            $ratio = $widthratio;
        } elseif ($RESIZEHEIGHT) {
            $ratio = $heightratio;
        }


    } else {
        $ratio = 1;
	}
	$newwidth = $width * $ratio;
	$newheight = $height * $ratio;
    if (function_exists("imagecopyresampled")) {
        $newim = imagecreatetruecolor($newwidth, $newheight);
        imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    } else {
        $newim = imagecreate($newwidth, $newheight);
        imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    }
    return $newim;
}

/**
 * @author jzh
 * @copyright 2008
 * @var $im image
 * 		$water 	water string or water image
 * 		$mode 	0 from string; 1 from file   
 */
function CreateWaterImage($im, $water, $mode = 0)
{
    $width = imagesx($im);
    $height = imagesy($im);
    $newim = imagecreatetruecolor($width,$height);
	$white=imagecolorallocate($newim,255,255,255);
	$black=imagecolorallocate($newim,0,0,0); 
	$red=imagecolorallocate($newim,255,0,0);	
	imagefill($newim,0,0,$white); 
	imagecopy($newim,$im,0,0,0,0,$width,$height);
	switch($mode)
    { 
        case 0:   //from string
        imagestring($newim,2,0,0,$water,$white); 
        break;

        case 1:   //from file
        imagecopy($newim,$water,0,0,0,0,185,115);
        break;
     }
    return $newim;
}

?>